{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-08-28T07:09:12.709489Z",
     "iopub.status.busy": "2020-08-28T07:09:12.709083Z",
     "iopub.status.idle": "2020-08-28T07:09:20.184138Z",
     "shell.execute_reply": "2020-08-28T07:09:20.183359Z",
     "shell.execute_reply.started": "2020-08-28T07:09:12.709446Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use \"pip install psycopg2-binary\" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.\n"
     ]
    }
   ],
   "source": [
    "%matplotlib widget\n",
    "import requests\n",
    "from bmutils.series import generate_series,generate_series_dicom,SeriesClass\n",
    "from bmutils.pipelines import SortSeriesByOrientation\n",
    "from bmmodule.utils import get_required_parameters, protocols\n",
    "import pydicom\n",
    "from brain_ctp.ctp_api import CTPModel\n",
    "#from bmutils.pipelines import *\n",
    "#from brain_ctp.ctp_new.airefmodel.auxiliary import IntegrationPipeline\n",
    "requests.get('http://192.168.2.44:7000/api/status')\n",
    "\n",
    "CTP_folder =\"/Resources/Biomind_CTP_01_/\"\n",
    "#CTP_folder = '/biomind/tmp/tmpxihbf87s/files/pacs_rest/drawers/dicom_folders_hot/67f26326f10e4bffad6dfd80a940b3883'\n",
    "series=generate_series(CTP_folder)\n",
    "#series\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_series=series\n",
    "ctp_series_uid=list(all_series.keys())[0]\n",
    "#ctp_series=all_series[ctp_series_uid]\n",
    "pay_load={\n",
    "    'pdicom_folder':CTP_folder,\n",
    "    'planguage':'en-us',\n",
    "    'pconfig':{'ctp':{}},\n",
    "    'pseries_classifier':{\n",
    "        SeriesClass.CTP.key:ctp_series_uid\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1.2.840.113619.2.416.263797347391843930758744724964937820965.35.000000512512.vol<1,16>'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ctp_series_uid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'CTP': '1.2.840.113619.2.416.263797347391843930758744724964937820965.35.000000512512.vol<1,16>'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series, series_classifier, metadata = get_required_parameters(pay_load)\n",
    "series_classifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tensorrt_server = \"http://192.168.2.44:7000\"\n",
    "res = CTPModel(tensorrt=tensorrt_server)(pay_load)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-08-28T07:10:33.551396Z",
     "iopub.status.busy": "2020-08-28T07:10:33.550748Z",
     "iopub.status.idle": "2020-08-28T07:10:33.558762Z",
     "shell.execute_reply": "2020-08-28T07:10:33.557829Z",
     "shell.execute_reply.started": "2020-08-28T07:10:33.551325Z"
    }
   },
   "outputs": [],
   "source": [
    "list(all_series.keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# f = pydicom.read_file('/Resources/Biomind_CTP_01_/d4a483f2-e3e975b3-a1861dac-f489f1db-1b8af0f0')\n",
    "f = pydicom.dcmread('/Resources/Biomind_CTP_01_/d4a483f2-e3e975b3-a1861dac-f489f1db-1b8af0f0')\n",
    "f"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-08-28T07:10:50.594934Z",
     "iopub.status.busy": "2020-08-28T07:10:50.594480Z",
     "iopub.status.idle": "2020-08-28T07:10:50.603626Z",
     "shell.execute_reply": "2020-08-28T07:10:50.601989Z",
     "shell.execute_reply.started": "2020-08-28T07:10:50.594889Z"
    }
   },
   "outputs": [],
   "source": [
    "def recover_seriesuid_instances(series, series_classifier, series_class, metadata):\n",
    "    if series_class in series_classifier and len(series[series_classifier[series_class]]) > 0:\n",
    "        series_uid = metadata[series_classifier[series_class]]['SeriesInstanceUID']\n",
    "        instances = flatten([files for uid, files in series.items() if series_uid in uid])\n",
    "        return series_uid, instances\n",
    "\n",
    "    return None, None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-08-28T07:13:13.853552Z",
     "iopub.status.busy": "2020-08-28T07:13:13.853267Z",
     "iopub.status.idle": "2020-08-28T07:13:20.727129Z",
     "shell.execute_reply": "2020-08-28T07:13:20.726357Z",
     "shell.execute_reply.started": "2020-08-28T07:13:13.853523Z"
    }
   },
   "outputs": [],
   "source": [
    "from copy import deepcopy\n",
    "from bmmodule.utils import get_required_parameters\n",
    "from bmutils.utils import npy2str, str2npy, flatten\n",
    "series, series_classifier, metadata = get_required_parameters(pay_load)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "SeriesClass.CTP.key in series_classifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "series[series_classifier[ SeriesClass.CTP.key]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "series_by_orientation = deepcopy(SortSeriesByOrientation()(series))\n",
    "ctp_volume_uid = series_classifier.get(SeriesClass.CTP.key)\n",
    "ctp_series_uid, ctp_instances = recover_seriesuid_instances(series_by_orientation, series_classifier, SeriesClass.CTP.key,\n",
    "                                                            metadata)\n",
    "#instance_uids = payload[protocols.volumes][ctp_volume_uid]\n",
    "sortinstance = [pydicom.read_file(s).get(\"SOPInstanceUID\") for s in ctp_instances ]\n",
    "pay_load.update({\n",
    "    'psorted_series': sortinstance\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-08-28T07:19:41.941580Z",
     "iopub.status.busy": "2020-08-28T07:19:41.941185Z",
     "iopub.status.idle": "2020-08-28T07:19:41.963650Z",
     "shell.execute_reply": "2020-08-28T07:19:41.962929Z",
     "shell.execute_reply.started": "2020-08-28T07:19:41.941540Z"
    }
   },
   "outputs": [],
   "source": [
    "sortinstance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "res=IntegrationPipeline(mode = 'production',tensorrt=tensorrt_server)(pay_load)\n",
    "#inputs={'pseries':all_series,'pprediction':res['pprediction']}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
